﻿using Core.Web.Controllers.Order.AcceptModel;
using Core.Web.Model;
using Core.DataAccess.Model.Project.Order;
using Core.Framework.Model.Common;
using Core.Framework.Util;
using Core.IBusiness.IOrderModule;
using Core.IBusiness.IOrderModule.Model;
using Core.IBusiness.ISDK;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

namespace Core.Web.Controllers.Order
{
    /// <summary>
    /// 订单管理
    /// </summary>
    [Route(".v1/Order/[controller]")]
    [ApiController]
    [ApiVerification(IBusiness.IProjectModule.Model.APIServiceEnum.Shop)]
    public class OrderBasisController : BaseApiController
    {
        public OrderBasisController(IOrder iOrder, IPay iPay)
            : base(iOrder: iOrder, iPay: iPay)
        {
        
        }
        
            

        /// <summary>
        /// 查询指定订单详情
        /// </summary>
        /// <param name="order_key">订单关键字</param>
        /// <returns></returns>
        [HttpGet("{order_key}")]
        public IResult GetOrderItem(int order_key)
        {

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.GetOrderItem(
                          order_key,
                          iProjectInfo.Token));
                  }, new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null"));
        }


        /// <summary>
        /// 查询指定订单详情包含物流信息
        /// </summary>
        /// <param name="order_key">订单关键字</param>
        /// <returns></returns>
        [HttpGet(".track{order_key}")]
        public IResult GetOrderAndTrackNode(int order_key)
        {

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.GetOrderItem(
                          order_key,
                          iProjectInfo.Token,
                          true));
                  }, new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null"));
        }
        

        /// <summary>
        /// 查询指定用户订单统计
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IResult GetOrderCount()
        {

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.GetOrderCount(
                          iClientInfo.Key,
                          iProjectInfo.Token));
                  });
        }

        /// <summary>
        /// 查询用户订单
        /// </summary>
        /// <param name="model">额外事件实体</param>
        /// <returns></returns>
        [HttpPost(".order")]
        public IResult GetOrder(OrderListRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.GetOrder(
                          iProjectInfo.Token,
                          model.order_statu,
                          model.order_type,
                          model.pagination), model.pagination);
                  });
        }


        /// <summary>
        /// 查询指定用户订单
        /// </summary>
        /// <param name="model">额外事件实体</param>
        /// <returns></returns>
        [HttpPost(".orderuser")]
        public IResult GetOrderUser(OrderListRequest model)
        {

            model.LayerTable();

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.GetOrderUser(
                          iClientInfo.Key,
                          iProjectInfo.Token,
                          model.order_statu,
                          model.order_type,
                          model.pagination), model.pagination);
                  });
        }



        /// <summary>
        /// 下订单
        /// </summary>
        /// <param name="request">订单请求信息</param>
        /// <returns></returns>
        [HttpPost(".regOrder")]
        public IResult UserCartOrder(OrderRequest request)
        {

            request.Page(this.iClientInfo, iProjectInfo);

            return this.ExecMethod(
               () => {
                   return this.Result(this.iOrder.PlaceTheOrder(
                       request.orderBasis,
                       request.products,
                       request.address));
               }
               , request.orderBasis.IsValid(IsValidEnum.reg)
               , request.products.IsValid(IsValidEnum.reg)
               , request.address.IsValid(IsValidEnum.reg));
        }



        /// <summary>
        /// 订单发起付款
        /// [支付宝]
        /// </summary>
        /// <returns></returns>
        [HttpPost(".aliApplyPay")]
        public IResult AliApplyPay(AliApplyPayRequest request)
        {
            if (iProjectInfo.PayAliInfo == null)
            {
                return new ApiResult
                {
                    code = CodeResult.CONFIG_ERROR
                };
            }

           var result = this.iPay.AliShopOrderPay(request.orderKey, iProjectInfo);

            if (result.IsFinished)
            {
                return new ApiResult
                {
                    code = CodeResult.SUCCESS,
                    date = new {
                        price = result.Date.Key,
                        orderInfo = result.Date.Value
                    }
                };
            }

            return new ApiResult
            {
                code = CodeResult.DATA_NOT_FOUND,
                info = result.Discription
            };

        }

        /// <summary>
        /// 订单发起付款
        /// [微信]
        /// </summary>
        /// <returns></returns>
        [HttpPost(".wChatApplyPay")]
        public IResult WChatApplyPay(AliApplyPayRequest request)
        {
            if (iProjectInfo.PayWChatInfo == null)
            {
                return new ApiResult
                {
                    code = CodeResult.CONFIG_ERROR
                };
            }

            MethodResult<object> result = new MethodResult<object>();

            if (this.iClientInfo.clientType == ClientTypeEnum.WChat)
            {
                var resultMin = this.iPay.MinWChatShopOrderPay(request.orderKey);
                result.IsFinished = resultMin.IsFinished;
                result.Discription = resultMin.Discription;
                result.Date = resultMin.Date;
            }
            else
            {
                var resultApp = this.iPay.WChatShopOrderPay(request.orderKey, iProjectInfo);
                result.IsFinished = resultApp.IsFinished;
                result.Discription = resultApp.Discription;
                result.Date = resultApp.Date;
            }


            if (result.IsFinished)
            {
                return new ApiResult
                {
                    code = CodeResult.SUCCESS,
                    date = result.Date 
                    //new
                    //{
                    //    price = result.Date.Key,
                    //    orderInfo = result.Date.Value
                    //}
                };
            }

            return new ApiResult
            {
                code = CodeResult.DATA_NOT_FOUND,
                info = result.Discription
            };

        }



        /// <summary>
        /// 订单已付款
        /// </summary>
        /// <param name="payment_ordel">付款实体</param>
        /// <returns></returns>
        [HttpPost(".payment")]
        public IResult OrderPayment(OrderPay payment_ordel)
        {

            payment_ordel.UserKey = iClientInfo.Key;

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderPayment(
                          payment_ordel));
                  }
                  , payment_ordel.IsValid(IsValidEnum.reg));
        }


        /// <summary>
        /// 订单产品发货
        /// </summary>
        /// <param name="request">发货商品</param>
        /// <returns></returns>
        [HttpPost(".shipment")]
        public IResult OrderShipments(OrderShipmentsRequest request)
        {

            request.Page(iClientInfo);

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderShipments(
                          request.shipmentses));
                  }
                  , request.shipmentses.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 订单产品发货确认
        /// </summary>
        /// <param name="order_key">订单关键字</param>
        /// <returns></returns>
        [HttpPost(".shipmentconfirm{order_key}")]
        public IResult OrderShipmentsConfirm(int order_key)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderShipmentsConfirm(
                          order_key,
                          iClientInfo.Key,
                          DateTime.Now));
                  }
                  , new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null"));
        }


        /// <summary>
        /// 订单退货申请
        /// </summary>
        /// <param name="request">申请退货实体</param>
        /// <returns></returns>
        [HttpPost(".returnsapply")]
        public IResult OrderReturnsApply(OrderReturnsRequest request)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderReturnsApply(
                          request.returnses));
                  }
                  , request.IsValid(IsValidEnum.reg));
        }

        /// <summary>
        /// 退货订单发货
        /// </summary>
        /// <param name="request">退货订单</param>
        /// <returns></returns>
        [HttpPost(".returnshipment")]
        public IResult OrderReturns(OrderShipmentsRequest request)
        {
            request.Page(iClientInfo);

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderReturns(
                          request.shipmentses));
                  }
                  , request.shipmentses.IsValid(IsValidEnum.reg));
        }


        /// <summary>
        /// 订单退款申请
        /// </summary>
        /// <param name="refund">申请实体</param>
        /// <returns></returns>
        [HttpPost(".refundapply")]
        public IResult OrderRefundApply(OrderRefund refund)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderRefundApply(
                          refund));
                  }
                  , refund.IsValid(IsValidEnum.reg));
        }



        /// <summary>
        /// 订单申请售后
        /// </summary>
        /// <param name="model">售后实体</param>
        /// <returns></returns>
        [HttpPost(".serveapply")]
        public IResult OrderServeApply(OrderServe model)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderServeApply(
                          model));
                  }
                  , model.IsValid(IsValidEnum.reg));
        }



        /// <summary>
        /// 订单额外事件
        /// </summary>
        /// <param name="model">额外事件实体</param>
        /// <returns></returns>
        [HttpPost(".accident")]
        public IResult OrderAccident(OrderAccident model)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderAccident(
                          model));
                  }
                  , model.IsValid(IsValidEnum.reg));
        }



        







        /// <summary>
        /// 修改售后相关信息
        /// </summary>
        /// <param name="order_serve_key">售后关键字</param>
        /// <param name="content">售后实体</param>
        /// <returns></returns>
        [HttpPost(".serveapply{order_serve_key}/{content}")]
        public IResult OrderServeUpdate(int order_serve_key, string content)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderServeUpdate(
                          order_serve_key, content));
                  }
                  , new Tuple<bool, string>(order_serve_key > 0, order_serve_key > 0 ? "" : "params: order_key is null")
                  , new Tuple<bool, string>(!string.IsNullOrWhiteSpace(content), string.IsNullOrWhiteSpace(content) ? "params: content is null" : ""));
        }








        /// <summary>
        /// 退款申请状态处理
        /// </summary>
        /// <param name="order_key">订单关键字</param>
        /// <param name="status">状态枚举</param>
        /// <returns></returns>
        [HttpPost(".refundapply{order_key}/{status}")]
        public IResult OrderRefundStuas(int order_key, OrderBitStatu status)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderRefundStuas(
                          order_key, status, DateTime.Now));
                  }
                  , new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null"));
        }




        /// <summary>
        /// 修改退款订单金额
        /// </summary>
        /// <param name="order_key">订单关键字</param>
        /// <param name="order_refund_key">退款订单关键字</param>
        /// <param name="price">修改价格</param>
        /// <returns></returns>
        [HttpPut(".refundupdate{order_key}/{order_refund_key}/{price}")]
        public IResult OrderRefundUpdate(int order_key, int order_refund_key, decimal price)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderRefundUpdate(
                          order_key, order_refund_key, price));
                  }
                  , new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null"));
        }



        /// <summary>
        /// 修改地址
        /// </summary>
        /// <param name="order_key">订单关键字</param>
        /// <param name="content">地址信息</param>
        /// <returns></returns>
        [HttpPut(".address{order_key}/{content}")]
        public IResult OrderRefundUpdate(int order_key, string content)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderAddressUpdate(
                          order_key, content));
                  }
                  , new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null")
                  , new Tuple<bool, string>(!string.IsNullOrWhiteSpace(content), string.IsNullOrWhiteSpace(content) ? "params: content is null" : ""));
        }


        /// <summary>
        /// 订单评论
        /// </summary>
        /// <param name="orderproduct_key">订单商品关键字[非产品ID]</param>
        /// <returns></returns>
        [HttpPut(".comment{orderproduct_key}")]
        public IResult OrderComment(int orderproduct_key)
        {

            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderComment(orderproduct_key));
                  });
        }


        /// <summary>
        /// 修改指定产品收货地址
        /// </summary>
        /// <param name="order_key">订单关键字</param>
        /// <param name="content">地址信息</param>
        /// <param name="product_key">订单产品关键字</param>
        /// <returns></returns>
        [HttpPut(".addressitem{order_key}/{content}/{product_key}")]
        public IResult OrderAddressUpdate(int order_key, string content, int product_key)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderAddressUpdate(
                          order_key, content, product_key));
                  }
                  , new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null")
                  , new Tuple<bool, string>(!string.IsNullOrWhiteSpace(content), string.IsNullOrWhiteSpace(content) ? "params: content is null" : "")
                  , new Tuple<bool, string>(product_key > 0, product_key > 0 ? "" : "params: product_key is null")
                  );
        }




        /// <summary>
        /// 修改订单付款金额
        /// </summary>
        /// <param name="order_key">订单关键字</param>
        /// <param name="price">修改价格</param>
        /// <returns></returns>
        [HttpPut(".payment{order_key}/{price}")]
        public IResult OrderPaymentUpdate(int order_key, decimal price)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.OrderPaymentUpdate(
                          order_key,
                          price,
                          iProjectInfo.Token));
                  }
                  , new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null")
                  , new Tuple<bool, string>(price > 0, price > 0 ? "" : "params: price is null"));
        }





        /// <summary>
        /// 删除指定订单
        /// </summary>
        /// <param name="order_key"></param>
        /// <returns></returns>
        [HttpDelete(".cancel{order_key}")]
        public IResult CancelOrder(int order_key)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.CancelOrder(
                          order_key,
                          iProjectInfo.Token));
                  }
                  , new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null"));
        }


        /// <summary>
        /// 删除指定订单
        /// </summary>
        /// <param name="order_key"></param>
        /// <returns></returns>
        [HttpDelete(".d{order_key}")]
        public IResult delete(int order_key)
        {
            return this.ExecMethod(
                  () => {
                      return this.Result(this.iOrder.DeleteOrder(
                          order_key,
                          iProjectInfo.Token));
                  }
                  , new Tuple<bool, string>(order_key > 0, order_key > 0 ? "" : "params: order_key is null"));
        }

    }
}